﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace autoctrl.utils
{
  internal class SerialPortHelper
  {

    public enum OutputStrTypes
    {
      HEX,
      ASCII
    };

    public static String dataParse(SerialPort sp, OutputStrTypes ot)
    {
      String res;
      try
      {
        int byteCount = sp.BytesToRead;
        if (byteCount == 0) return "";
        byte[] buffer = new byte[byteCount];
        sp.Read(buffer, 0, byteCount);


        if (ot == OutputStrTypes.HEX)
        {
          res = string.Join(" ", buffer.Select(b => b.ToString("X2")));
        }
        else
        {
          res = Encoding.ASCII.GetString(buffer);

        }

      }
      catch (Exception ex)
      {
        return "";
      }
      return res;
    }
  }
}
